package com.bawei.a2010douyin.db.dao;

import android.content.Context;

import com.bawei.a2010douyin.entity.User;
import com.bawei.a2010douyin.week3.entity.Video;

public class DBUtils {
    private DBUtils(){}
    private static DBUtils dbUtils;
    public static DBUtils getInstance(){
        if(dbUtils == null){
            synchronized (DBUtils.class){
                if(dbUtils == null){
                    dbUtils = new DBUtils();
                }
            }
        }
        return dbUtils;
    }
    private DaoSession mDaoSession;
    private UserDao mUserDao;
    private VideoDao mVideoDao;
    //初始化：必须在application初始化
    public void init(Context context){
        mDaoSession = DaoMaster.newDevSession(context,"2010douyin");
        mUserDao = mDaoSession.getUserDao();
        mVideoDao = mDaoSession.getVideoDao();

    }
    //插入数据
    public void insert(User user){
        mUserDao.insertOrReplace(user);
    }
    //返回当前的余额
    public float getMoney(){
        return mUserDao.loadAll().get(0).getMoney();
    }
    //带条件
//    public float getMoney(String username){
//        return mUserDao.queryRaw("where username = ?",username).get(0).getMoney();
//
//    }
    //更新余额
    public void updateMoney(User user){
        mUserDao.update(user);//必须有主键
    }

    //缓存视频
    public void insertVideo(Video video){
        mVideoDao.insertOrReplace(video);
    }


}
